package com.surfilter.controller;

import com.surfilter.service.UploadService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: lijia
 * @Date: Created in  2018-05-07 14:34
 * @Description:
 */
@Controller
public class UploadController {

    Logger logger = LoggerFactory.getLogger(this.getClass().getName());

    @Autowired
    private UploadService uploadService;

    @RequestMapping("/upload")
    public String topage(){
        return "upload";
    }

    @RequestMapping(value = "/upload/batchImport",method = RequestMethod.POST)
    @ResponseBody
    public Object batchImport(@RequestParam("filename") MultipartFile file, String account) throws IOException {
        Map map = new HashMap();

        //判断文件是否为空
        if(file==null){
            map.put("flag",0);
//            map.put("msg","文件为空！");
            return map;
        }

        //获取文件名
        String fileName=file.getOriginalFilename();

        //验证文件名是否合格
        if(!account.equals(fileName.split("\\.")[0])){
            map.put("flag",1);
//            map.put("msg","上传信息和选中信息不匹配");
            return map;
        }

        //批量导入
        String message1 = uploadService.batchImport(account,file,false);
        String delmsg = message1.split("-")[1];
        String insertmsg = message1.split("-")[0];
        map.put("flag",2);
        map.put("num",insertmsg);
//        map.put("msg","成功覆盖"+delmsg+"条数据！<br>成功插入"+insertmsg+"条数据！");
        return map;
    }
}
